package com.yulebaby.callsenter.ui.activity;


import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.yulebaby.callsenter.R;
import com.yulebaby.callsenter.adapter.PreSaleRemarkAdapter;
import com.yulebaby.callsenter.bean.CustomerBean;
import com.yulebaby.callsenter.bean.PreSaleRemarkBean;
import com.yulebaby.callsenter.bean.multiplebean.MultipleCustomerBean;
import com.yulebaby.callsenter.event.EventFactory;
import com.yulebaby.callsenter.event.QuitEvent;
import com.yulebaby.callsenter.event.RefreshEvent;
import com.yulebaby.callsenter.global.Constant;
import com.yulebaby.callsenter.http.HttpHelper;
import com.yulebaby.callsenter.http.callback.HttpCallback;
import com.yulebaby.callsenter.task.RequestPreSaleRemarkTask;
import com.yulebaby.callsenter.ui.base.BaseActivity;
import com.yulebaby.callsenter.ui.view.YuLeBaByLinearLayoutManager;
import com.yulebaby.callsenter.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 查看跟踪记录界面
 */
public class PreSaleRemarkAcitivity extends BaseActivity {


    @BindView(R.id.iv_searchback)
    ImageView mIvSearchback;
    @BindView(R.id.tb_title)
    Toolbar mTbTitle;
    @BindView(R.id.pb_showProgress)
    ProgressBar mPbShowProgress;
    @BindView(R.id.page_iv)
    ImageView mPageIv;
    @BindView(R.id.btn_reload)
    Button mBtnReload;
    @BindView(R.id.fl_fail)
    RelativeLayout mFlFail;
    @BindView(R.id.btn_addpresale)
    Button mBtnAddpresale;
    @BindView(R.id.rv_showpresale)
    RecyclerView mRvShowpresale;
    @BindView(R.id.ll_showresult)
    LinearLayout mLlShowresult;

    private CustomerBean customerbean;
    private ArrayList<PreSaleRemarkBean> mPreSaleRemarkList;
    private PreSaleRemarkAdapter mAdapter;

    @Override
    protected int initLayoutId() {
        return R.layout.activity_pre_sale_remark;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        EventBus.getDefault().register(this);
        mPreSaleRemarkList = new ArrayList<>();
        showView(mPbShowProgress);

        mRvShowpresale.setHasFixedSize(true);
        mRvShowpresale.setLayoutManager(new YuLeBaByLinearLayoutManager(this));
        mAdapter = new PreSaleRemarkAdapter(mPreSaleRemarkList);

        mRvShowpresale.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
                PreSaleRemarkBean bean = (PreSaleRemarkBean) baseQuickAdapter.getItem(position);
                switch (view.getId()) {
                    case R.id.rl_trackprogress:
                        if (bean.getTrack_pro() > 3) {
                            Intent intent = new Intent(PreSaleRemarkAcitivity.this, LookOverTrackProgressActivity.class);
                            intent.putExtra(Constant.PRE_VISIT_TIMES, bean.getVisit_times());
                            intent.putExtra(Constant.CUSTOMER_ID, bean.getCustomer_id());
                            startActivity(intent);
                        }
                        break;
                }
            }
        });

        mRvShowpresale.setAdapter(mAdapter);

        Intent intent = getIntent();
        customerbean = (CustomerBean) intent.getSerializableExtra(Constant.CUSTOMER_BEAN);
        requestData();
    }

    /**
     * 请求数据
     */
    private void requestData() {
        showView(mPbShowProgress);
        if (customerbean != null) {
            RequestPreSaleRemarkTask.doRequestPreSaleRemarkTask(this, customerbean, new HttpCallback<List<PreSaleRemarkBean>>() {
                @Override
                public void onSuccess(List<PreSaleRemarkBean> result) {
                    processResult(result);
                }

                @Override
                public void onFail(String exception) {
                    super.onFail(exception);
                    showView(mFlFail); //显示失败界面
                }

                @Override
                public void onFailParam(String info) {
                    processResult(null);
                }
            });
        } else {
            showView(mFlFail);
        }
    }

    /**
     * 对结果进行处理
     *
     * @param result
     */
    private void processResult(List<PreSaleRemarkBean> result) {
        mPreSaleRemarkList.clear();
        if (result != null) {
            mPreSaleRemarkList.addAll(result);
            //发送通知信息界面次数更改
            EventBus.getDefault().post(EventFactory.CreateMsgEvent(result.get(0).getVisit_times()));
            customerbean.setVisit_times(result.get(0).getVisit_times());
            customerbean.setTrack_pro(Long.valueOf(result.get(0).getTrack_pro()));
        }
        mAdapter.notifyDataSetChanged();
        showView(mLlShowresult);
    }


    @OnClick({R.id.iv_searchback, R.id.btn_reload, R.id.btn_addpresale})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_searchback:
                finish();
                break;
            case R.id.btn_reload:
                requestData();
                break;
            case R.id.btn_addpresale:
                if (customerbean != null) {
                    Intent intent = new Intent(this, AddPreSaleRemarkActivity.class);
                    intent.putExtra(Constant.CUSTOMER_BEAN, customerbean);
                    startActivity(intent);
                }
                break;
        }
    }

    /**
     * 接受刷新信息的请求
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshEvent(RefreshEvent event) {
        if (event != null) {
            requestData();
        }
    }

    /**
     * 接受退出的请求
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void quitEvent(QuitEvent event) {
        if (event != null) {
            HttpHelper.getInstance().cancel(this);
            finish();
        }
    }

    /**
     * 显示View
     */
    private void showView(View v) {
        hideAllView();
        v.setVisibility(View.VISIBLE);
    }

    /**
     * 隐藏所有的View
     */
    private void hideAllView() {
        mPbShowProgress.setVisibility(View.INVISIBLE);
        mFlFail.setVisibility(View.INVISIBLE);
        mLlShowresult.setVisibility(View.INVISIBLE);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
